﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IBatisNet.DataMapper;
using StateGrid.OperationLog;
using StateGrid;
using System.Xml;
using StateGrid.GlobalVariables;

namespace StateGrid.GDFG
{
    /// <summary>
    ///GDFG_DAO 的摘要说明
    /// </summary>
    public class GDFG_DAO
    {
        private ISqlMapper sqlMapper = Mapper.Instance();
        private const int FLBM_LEN = 12;
        
        /// <summary>
        /// 日志操作对象
        /// </summary>
        private OperationLog_DAO operationLogDAO = new OperationLog_DAO();

        /// <summary>
        /// 获取所有国电法规分类
        /// </summary>
        public IList<GDFG_FL> GetAllGDFG_FL()
        {
            return sqlMapper.QueryForList<GDFG_FL>("SelectAllGDFG_FL", null);
        }

        /// <summary>
        /// 获取所有国电法规篇名
        /// </summary>
        public IList<GDFG_PM> GetAllGDFG_PM()
        {
            return sqlMapper.QueryForList<GDFG_PM>("SelectAllGDFG_PM", null);
        }

        /// <summary>
        /// 按分类编码查找分类信息
        /// </summary>
        /// <param name="flbm"></param>
        /// <returns></returns>
        public GDFG_FL GetGDFG_FLByFlbm(String flbm)
        {
            return sqlMapper.QueryForObject<GDFG_FL>("SelectGDFG_FLByFlbm", flbm);
        }

        /// <summary>
        /// 按照分类编码名称查找分类信息
        /// </summary>
        /// <param name="flmc">分类名称</param>
        /// <returns></returns>
        public GDFG_FL GetGDFG_FLByFlmc(String flmc)
        {
            return sqlMapper.QueryForObject<GDFG_FL>("SelectGDFG_FLByFlmc", flmc);
        }

        /// <summary>
        /// 获取指定分类编码、出版年份的篇名数量
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <param name="fbnf">发布年份</param>
        public int GetCountSomeGDFG_PM(String flbm, String fbnf)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();

            String wjm = null;

            if (null == flbm || "".Equals(flbm))
            {
            }
            else
            {
                wjm = flbm;
                if (null != fbnf && fbnf.Length == 4)
                {
                    //补零
                    while (wjm.Length < FLBM_LEN) wjm = wjm + '0';
                    wjm += '-' + fbnf;
                }
            }

            if (null != wjm)
            {
                wjm += '%';
            }

            para.Add("wjm", wjm);
            return sqlMapper.QueryForObject<int>("CountSomeGDFG_PM", para);
        }

        /// <summary>
        /// 获取指定分类编码、出版年份的篇名数量
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <param name="fbnf">发布年份</param>
        /// <param name="start">起始位置</param>
        /// <param name="end">结束位置</param>
        /// <returns></returns>
        public IList<GDFG_PM> GetSomeGDFG_PM(String flbm, String fbnf, int start, int end)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();

            String wjm = null;

            if (null == flbm || "".Equals(flbm))
            {
            }
            else
            {
                wjm = flbm;
                if (null != fbnf && fbnf.Length == 4)
                {
                    while (wjm.Length < FLBM_LEN) wjm = wjm + '0';
                    wjm += '-' + fbnf;
                }
            }

            if (null != wjm)
            {
                wjm += '%';
            }

            para.Add("wjm", wjm);
            para.Add("start", start);
            para.Add("end", end);
            return sqlMapper.QueryForList<GDFG_PM>("SelectSomeGDFG_PM", para);
        }

        /// <summary>
        /// 获取指定文件名的篇名信息
        /// </summary>
        /// <param name="wjm">文件名</param>
        /// <returns></returns>
        public GDFG_PM GetGDFG_PMByWjm(String wjm)
        {
            return sqlMapper.QueryForObject<GDFG_PM>("SelectGDFG_PMByWjm", wjm);
        }

        /// <summary>
        /// 插入一条分类信息
        /// </summary>
        /// <param name="gdfg_fl">分类信息</param>
        /// <returns></returns>
        public Boolean InsertGDFG_FL(GDFG_FL gdfg_fl)
        {
            Boolean flag = true;
            try
            {
                sqlMapper.Insert("InsertGDFG_FL", gdfg_fl);
            }
            catch (Exception)
            {
                flag = false;
            }

            return flag;
        }

        /// <summary>
        /// 插入一条篇名信息
        /// </summary>
        /// <param name="gdfg_pm">篇名信息</param>
        /// <returns></returns>
        public Boolean InsertGDFG_PM(GDFG_PM gdfg_pm)
        {
            Boolean flag = true;
            OperationLogItem operationLog = this.GetOperationLogItem(gdfg_pm, "INSERT", null);
            try
            {
                sqlMapper.Insert("InsertGDFG_PM", gdfg_pm);
                this.operationLogDAO.insertLogItem(operationLog);
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 更新一条分类信息
        /// </summary>
        /// <param name="gdfg_fl">要更新的分类信息</param>
        /// <returns></returns>
        public Boolean UpdateGDFG_FL(GDFG_FL gdfg_fl)
        {
            Boolean flag = true;
            int back = sqlMapper.Update("UpdateGDFG_FL", gdfg_fl);
            if (back <= 0)
            {
                flag = false;
            }
            return flag;

        }
        /// <summary>
        /// 更新一条篇名信息
        /// </summary>
        /// <param name="gdfg_pm">要更新的篇名信息</param>
        /// <param name="uname">操作用户名</param>
        /// <returns></returns>
        public Boolean UpdateGDFG_PM(GDFG_PM gdfg_pm, String uname)
        {
            Boolean flag = true;
            int back = sqlMapper.Update("UpdateGDFG_PM", gdfg_pm);
            if (back <= 0)
            {
                flag = false;
            }
            if (flag)
            {
                OperationLogItem operationLog = this.GetOperationLogItem(gdfg_pm, "UPDATE", uname);
                this.operationLogDAO.insertLogItem(operationLog);
            }
            return flag;
        }

        /// <summary>
        /// 按分类编码删除分类信息
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <returns></returns>
        public Boolean DeleteGDFG_FL(String flbm)
        {
            Boolean flag = true;
            int back = sqlMapper.Delete("DeleteGDFG_FLByFlbm", flbm);
            if (back <= 0)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 按文件名删除篇名信息
        /// </summary>
        /// <param name="wjm">文件名</param>
        /// <param name="uname">操作用户名</param>
        /// <returns></returns>

        public int DeleteGDFG_PM(String wjm, String uname)
        {
            int result = 0;

            GDFG_PM gdfgPM = this.GetGDFG_PMByWjm(wjm);

            OperationLogItem operationLog = null;


            //切分文件名
            String[] str = wjm.Split('-');
            if (str.Length < 3)
            {
                return result;
            }
            //文件名前缀
            String wjmStr = str[0] + "-" + str[1]  + "-";

            //删除指定篇名及文件
            if (OperationFile.DeleteFile(wjm))
            {
                result = sqlMapper.Delete("DeleteGDFG_PMByWjm", wjm);
                operationLog = this.GetOperationLogItem(gdfgPM, "DELETE", uname);
                if (null != operationLog)
                {
                    this.operationLogDAO.insertLogItem(operationLog);
                }
            }

            ICollection<GDFG_PM> list = this.GetSomeGDFG_PM(str[0], str[1], 1, 1000);

            int ph = 1;
            String phString = null;
            if (null != list)
            {
                foreach (GDFG_PM temp in list)
                {
                    phString = FormatPH(ph++);
                    //如果当前文件名与新文件名一样则不做处理
                    if (temp.wjm.Equals(wjmStr + phString))
                    {
                        continue;
                    }
                    //如果当前文件名与新文件名不一致,则更新文件名及篇名信息
                    else
                    {
                        changeGDFGPMWjm(temp.wjm, wjmStr + phString);
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 修改已有的文件名。
        /// </summary>
        /// <param name="oldWjm">旧文件名</param>
        /// <param name="newWjm">新文件名</param>
        /// <returns>是否修改成功</returns>
        public bool changeGDFGPMWjm(String oldWjm, String newWjm)
        {
            if (null == oldWjm || null == newWjm)
            {
                return false;
            }

            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("oldwjm", oldWjm);
            para.Add("newwjm", newWjm);

            bool result = true;
            try
            {
                sqlMapper.Update("ChangeGDFG_PM_wjm", para);
                //修改文件名
                OperationFile.ReNameFile(oldWjm, newWjm);
            }
            catch
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 规范篇号
        /// </summary>
        /// <param name="ph">篇号</param>
        /// <returns>3位篇号</returns>
        private String FormatPH(int ph)
        {
            String phString = ph.ToString();
            while (phString.Length < 3)
            {
                phString = "0" + phString;
            }
            return phString + ".PDF";
        }

        /// <summary>
        /// 根据索引编码、出版年份获取最大文件的篇名信息
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <param name="fbnf">出版年份</param>
        /// <returns></returns>
        public GDFG_PM getMaxGDFG_PM(String flbm, String fbnf)
        {
            if (null == flbm || "".Equals(flbm))
            {
                return null;
            }
            if (null == fbnf || "".Equals(fbnf))
            {
                return null;
            }
            while (flbm.Length < FLBM_LEN)
            {
                flbm = flbm + "0";
            }
            //设置匹配文件名
            String wjm = flbm + "-" + fbnf + "%";
            return sqlMapper.QueryForObject<GDFG_PM>("SelectMaxGDFG_PM", wjm);
        }

        /// <summary>
        /// 生成日志信息
        /// </summary>
        /// <param name="zwqkPM">中文篇名信息</param>
        /// <param name="opType">操作类型</param>
        /// <param name="uname">用户名</param>
        /// <returns></returns>
        private OperationLogItem GetOperationLogItem(GDFG_PM gdfgPM, String opType, String uname)
        {
            if (null == gdfgPM)
            {
                return null;
            }

            OperationLogItem operationLogItem = new OperationLogItem();
            //操作类型
            operationLogItem.opType = opType;
            //数据库表
            operationLogItem.opTable = "GDFG_PM";
            //全文页数
            operationLogItem.opPageCount = gdfgPM.ys;

            operationLogItem.opObjectID = "国电法规";
            // 操作涉及的法规
            operationLogItem.opBookIn = "国电法规";

            // 时间
            operationLogItem.opDate = DateTime.Now;
            // 操作员
            operationLogItem.opPerson = uname;
            //记录操作模式，判断是否属于批量操作。取值范围SINGLE/BATCH。
            operationLogItem.opMode = "SINGLE";

            //操作的内容
            String content = "('" + gdfgPM.flbm + "','" + gdfgPM.sybm + 
                "','" + gdfgPM.wjm + "','" + gdfgPM.fgwh + 
                "','" + gdfgPM.fgmc + "','" + gdfgPM.fbdw + 
                "','" + gdfgPM.fbrq + "','" + gdfgPM.ssrq + 
                "','" + gdfgPM.sx + "','" + gdfgPM.sxrq + 
                "','" + gdfgPM.kb + "','" + gdfgPM.ys + 
                "','" + gdfgPM.bz + "','" + gdfgPM.zyfl + 
                "','" + gdfgPM.qwjc + "','" + gdfgPM.kf + 
                "','" + gdfgPM.gxrq + "','" + gdfgPM.gxx + 
                "','" + gdfgPM.dlr + "')";
            operationLogItem.opContent = content;
            return operationLogItem;
        }

        public GDFG_DAO()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
    }
}